package com.bstek.demo.urule.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bstek.demo.urule.entity.SysUser;
import com.bstek.demo.urule.mapper.SysUserMapper;
import com.bstek.urule.console.database.model.Group;
import com.bstek.urule.console.database.model.User;
import com.bstek.urule.console.database.service.project.ProjectService;
import com.bstek.urule.console.database.service.user.UserService;
import com.fawkes.core.utils.crypto.sm.Sm4Tool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author xuetieqi
 * @date 2023/2/27 4:24 PM
 * @desc 凤翎单点登陆urule
 **/
@Service(UserService.BEAN_ID)
public class MyUserService implements UserService {
    @Autowired
    SysUserMapper sysUserMapper;

    @Override
    public User get(String account) {

        LambdaQueryWrapper<SysUser> lambda = new LambdaQueryWrapper<>();
        lambda.eq(SysUser::getUserName, account).last("limit 1");
        SysUser sysUser = sysUserMapper.selectOne(lambda);
        if (sysUser != null) {
            //com.bstek.urule.console.database.service.group.GroupService.ins.addGroupUser("sxdt", account);
            User user = new User();
            user.setId(sysUser.getUserName());
            user.setName(sysUser.getUserFullname());
            user.setPassword(sysUser.getPassword());
            user.setEnable(true);
            user.setCreateDate(sysUser.getCreateDate());
            user.setUpdateDate(sysUser.getUpdateDate());
            user.setEmail(sysUser.getEmail());
            user.setCreateUser(sysUser.getCreateBy());
            user.setUpdateUser(sysUser.getUpdateBy());
            List<Group> groups = com.bstek.urule.console.database.manager.group.GroupManager.ins.createQuery().list(user.getId());
            user.setGroups(groups);
            return user;
        }
        return null;
    }

    @Override
    public User validate(String account, String password) {
        String encrypt = Sm4Tool.encrypt("43869399C1411A3BE71A99B35123AEFC", password);
        LambdaQueryWrapper<SysUser> lambda = new LambdaQueryWrapper<>();
        lambda.eq(SysUser::getUserName, account)
                .last("limit 1");
        SysUser sysUser = sysUserMapper.selectOne(lambda);
        if (sysUser == null) {
            throw new RuntimeException("账号不存在");
        }
        boolean flag = (new BCryptPasswordEncoder()).matches(encrypt, sysUser.getPassword());
        if (!flag) {
            throw new RuntimeException("密码错误");
        }
        com.bstek.urule.console.database.service.group.GroupService.ins.addGroupUser("sxdt", account);
        ProjectService.ins.addProjectuser(1, account);
        User user = new User();
        user.setId(String.valueOf(sysUser.getUserName()));
        user.setName(sysUser.getUserFullname());
        user.setPassword(sysUser.getPassword());
        user.setEnable(true);
        user.setCreateDate(sysUser.getCreateDate());
        user.setUpdateDate(sysUser.getUpdateDate());
        user.setEmail(sysUser.getEmail());
        user.setCreateUser(sysUser.getCreateBy());
        user.setUpdateUser(sysUser.getUpdateBy());
        List<Group> groups = com.bstek.urule.console.database.manager.group.GroupManager.ins.createQuery().list(user.getId());
        user.setGroups(groups);
        return user;

    }


}
